from flickrmirror.view.base import IView
from flickrmirror.controller import MirrorController
from flickrmirror.tools import *
from frames import *
import wx
import threading

class GUIView:
    
    def __init__(self):
        self.controller = MirrorController(self)
        self.main_frame = MainFrame(self)
        
        self.update_information()
        
    def authenticate_button_clicked(self, event):
        self.controller.authenticate()
        self.update_information()
        
    def update_information(self):
        bd = self.controller.backup_directory
        
        if bd is None:
            self.main_frame.backup_directory_text.SetLabel("No folder selected.")
        else:
            self.main_frame.backup_directory_text.SetLabel(str(bd))
            

        authenticated = self.controller.authenticator.authenticated()
        
        if authenticated:
            self.main_frame.authenticate_text.SetLabel("You are authenticated with flickr.")
        else:
            self.main_frame.authenticate_text.SetLabel("You are NOT authenticated with flickr.")
            
    def run_mirror(self):
        
        class RunMirrorThread(threading.Thread):
        
            def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, view=None):
                self.view = view
                threading.Thread.__init__(self, group, target, name, args, kwargs)
        
            def run(self):
                self.view.started_mirroring()
                self.view.controller.mirror()
                self.view.ended_mirroring()
                
        thread = RunMirrorThread(view=self)
        thread.start()
        
    def started_mirroring(self):
        self.main_frame.mirror_button.started()
        self.main_frame.authenticate_button.Disable()
        self.main_frame.backup_directory_button.Disable()
        
    def ended_mirroring(self):
        self.main_frame.mirror_button.paused()
        self.main_frame.authenticate_button.Enable()
        self.main_frame.backup_directory_button.Enable()
        
    def mirror_button_clicked(self, event):
        
        if self.main_frame.mirror_button.active():
            self.ended_mirroring()
        else:
            if self.controller.authenticator.authenticated() and self.controller.backup_directory is not None:
                self.run_mirror()
            else:
                wx.MessageBox("You must be authenticated with flickr and select a backup location")
        
    def backup_directory_button_clicked(self, event):
        d = wx.DirDialog(self.main_frame, "Please enter the backup location")
        d.ShowModal()
        
        self.controller.backup_directory = d.GetPath()
        self.update_information()
        
    def prompt(self, msg, result=True):
        if result:
            wx.TextEntryDialog(self.main_frame, msg).GetValue().strip()
        else:
            wx.MessageBox(msg)
        
    def status(self, msg):
        self.main_frame.SetStatusText(msg)